package com.anli.Controller;

import com.anli.Service.EmptService;
import com.anli.pojo.Emp;
import com.anli.pojo.Result;
import com.anli.pojo.pageBean;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmptService emptService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDateTime end) {
        log.info("name,gender,begin,end");
        pageBean pb = emptService.page(page, pageSize, name, gender, begin, end);
        return Result.success(pb);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {
        emptService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result save(@RequestBody Emp emp) {
        emptService.save(emp);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Emp emp = emptService.getById(id);
        return Result.success(emp);
    }
    @PutMapping("/test")
    public Result update(@RequestBody Emp emp) {
        log.info("更新员工的信息"+emp);
        emptService.update(emp);
        return Result.success();
    }

}
